package com.cj.display_sys.fragment

import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cj.display_sys.BaseFragment
import com.cj.display_sys.MainActivity
import com.cj.display_sys.MainViewModel
import com.cj.display_sys.R
import com.cj.display_sys.http.Picture
import com.cj.display_sys.http.RowsItem
import com.cj.display_sys.utils.LongObserver
import com.cj.display_sys.utils.getTranslateAnim
import com.cj.display_sys.utils.interval
import com.cj.display_sys.utils.showImg
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_activity.*
import kotlinx.android.synthetic.main.fragment_introfuct.*
import kotlinx.android.synthetic.main.layout_img.view.*
import java.util.concurrent.TimeUnit

/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 *
 * Created by yi on 2021/6/15.
 */
class ActivityFragment : BaseFragment<MainViewModel>() {

    private lateinit var titleAdapter: BaseQuickAdapter<RowsItem, BaseViewHolder>

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_activity
    }

    override fun show() {
    }

    override fun findView() {

        titleAdapter = object : BaseQuickAdapter<RowsItem, BaseViewHolder>(R.layout.layout_news) {

            override fun convert(holder: BaseViewHolder, item: RowsItem) {
                holder.setText(R.id.tv_a_name, item.title)
                holder.setText(R.id.tv_a_date, item.releaseDate)
                holder.setText(R.id.tv_new_content, item.content)
            }

        }

        activity_recycler_view.apply {
            layoutManager = LinearLayoutManager(activity)
            setHasFixedSize(true)
            adapter = titleAdapter

        }
    }

    override fun init() {
        getLatestInfo()
        
    }

    fun getLatestInfo() {
        viewModel.getNews().observe(this, {
            titleAdapter.setNewInstance(it)
        })
        
        tenGet()
    }

    override fun setListener() {
        titleAdapter.setOnItemClickListener { adapter, view, position ->
            currentSeconds=0
            disposable?.dispose()
            val item = titleAdapter.getItem(position)
            setNewsInfo(item)
            cl_a_pop.isVisible = true
            ll_a_pop.startAnimation(getTranslateAnim(800, 1f, 0f))
        }

        cl_a_pop.setOnClickListener {
            currentSeconds=0
            disposable?.dispose()
            ll_a_pop.startAnimation(getTranslateAnim(800, 0f, 1f))
            cl_a_pop.postDelayed({
                cl_a_pop.isVisible = false
            }, 800)
            tenGet()
        }

        ll_activity_back.setOnClickListener {
            currentSeconds=0
            disposable?.dispose()
            (activity as MainActivity).indexMain()
            (activity as MainActivity).tenGet()
        }

    }

    private fun setNewsInfo(item: RowsItem) {
        tv_news_title.text = item.title
        tv_news_content.text = item.content
        tv_news_date.text = item.releaseDate
    }

    override fun providerVMClass(): Class<MainViewModel>? {
        return MainViewModel::class.java
    }

    var disposable: Disposable? = null
    var currentSeconds = 0

    //1、有紧急公告 不播放视频 播放音频 
    //2、普通公告  无操作十分钟之后播放视频
    fun tenGet() {
        currentSeconds = 0
        interval(1, 1, TimeUnit.SECONDS, object : LongObserver() {
            override fun onNext(t: Long) {
                currentSeconds += 1
//                tv_current_seconds3?.post {
//                    tv_current_seconds3?.text = currentSeconds.toString()
//                }
                //LogUtil.e("currentSeconds:$currentSeconds")
                if (currentSeconds == 600) {
                    activity?.runOnUiThread {
                        disposable?.dispose()
                        if ((activity as MainActivity).isNewsFast) {
                            //(activity as MainActivity).musicPlayer?.start()
                        } else {
                            (activity as MainActivity).showVideo()
                        }
                    }
                }
            }

            override fun onSubscribe(d: Disposable) {
                disposable = d
            }
        })
    }
    
    
}